/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.excel.convert;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.star.framework.excel.core.Column;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import lombok.Getter;
import lombok.Setter;

/**
 * 字段值包装器<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
public class ValueWrapper<T> implements Serializable {

  /** 原始值 */
  @Getter @Setter private T value;

  @Getter @Setter private Column column;

  /** 所在行数据 */
  @Getter @Setter private Map<String, Object> rowValues;

  /** 可能的异常 */
  @Getter @Setter @JsonIgnore private Throwable ex;

  public ValueWrapper() {}

  public ValueWrapper(T value) {
    this.value = value;
  }

  public ValueWrapper(T value, Column column, Map<String, Object> rowValues) {
    this.value = value;
    this.column = column;
    this.rowValues = rowValues;
  }

  /**
   * 判断是一个没有异常的值
   *
   * @return
   */
  public boolean isSuccess() {
    return ex == null;
  }

  /**
   * 将数据与表头对应
   *
   * @param header 表头
   * @param data 数据
   * @return
   */
  public static List<Map<Integer, ValueWrapper<?>>> wrapper(
      Map<Integer, Column> header, Collection<?> data) {
    if (CollectionUtil.isNotEmpty(data)) {
      List<Map<Integer, ValueWrapper<?>>> valueWrapperMaps = new ArrayList<>(data.size());
      for (Object bean : data) {
        if (bean != null) {
          Map<Integer, ValueWrapper<?>> valueWrapperMap = wrapper(header, bean);
          valueWrapperMaps.add(valueWrapperMap);
        }
      }

      return valueWrapperMaps;
    }

    return new ArrayList<>();
  }

  /**
   * 将数据与表头对应
   *
   * @param header 表头
   * @param data 数据
   * @return
   */
  private static Map<Integer, ValueWrapper<?>> wrapper(Map<Integer, Column> header, Object data) {
    Map<String, Object> beanMap = BeanUtil.beanToMap(data);
    Map<Integer, ValueWrapper<?>> valueWrapperMap = new HashMap<>(beanMap.size());
    beanMap.forEach(
        (k, v) -> {
          Integer index =
              header.entrySet().stream()
                  .filter(entry -> entry.getValue().getKey().equals(k))
                  .map(Entry::getKey)
                  .findFirst()
                  .orElse(null);
          if (index != null) {
            Column column = header.get(index);
            valueWrapperMap.put(index, new ValueWrapper<>(v, column, beanMap));
          }
        });

    return valueWrapperMap;
  }

  @Override
  public String toString() {
    return "ValueWrapper{ " + "value = " + value + " }";
  }
}
